package com.zhaopin.thread;

/**
 * @author yin.peide
 * @date 2021-06-23 10:24
 * 活锁
 */
public class ALiveLock {


    static volatile int count = 10;
    final static Object lock = new Object();

    public static void main(String[] args) {

        new Thread(() -> {

            while (count > 0){
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lock){
                    count --;
                }

                System.out.println("t1:" + count);
            }
        },"t1").start();



        new Thread(() -> {

            while (count < 20){
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lock){
                    count ++;
                }

                System.out.println("t2:" + count);
            }
        },"t2").start();



    }
}
